package com.ysd.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.entity.Modules;
import com.ysd.entity.RoleModules;
import com.ysd.service.ModulesService;
import com.ysd.service.RoleModulesService;
import com.ysd.util.Result;

@RestController
public class ModulesController {
	@Autowired
	ModulesService ms;
	@Autowired
	RoleModulesService rs;
	
	@RequestMapping("selTree")
	public Object selTree() {
		List<Modules> list = ms.findTree();
		return list;
	}
	
	@RequestMapping("addTree")
	public Object addTree(Modules modules) {
		Modules m = ms.findName(modules);
		if (m == null) {
			int n = ms.insertTree(modules);
			if (n>0) {
				return Result.toClient("0", "添加成功");
			}else {
				return Result.toClient("1", "添加失败");
			}
		}
		return Result.toClient("1", "该模块已存在!!!");
	}
	
	@RequestMapping("updateTree")
	public Object updateTree(Modules modules) {
		Modules m = ms.findName(modules);
		if (m == null) {
			int n = ms.modifyTree(modules);
			if (n>0) {
				return Result.toClient("0", "修改成功");
			} else {
				return Result.toClient("1", "修改失败");
			}
		}
		return Result.toClient("1", "该模块已存在!!!");
	}
	
	@RequestMapping("getParentId")
	public Object getParentId(Modules modules) {
		return ms.findParentId(modules);
	}
	
	@RequestMapping("delTree")
	public Object delTree(Modules modelues,int id) {
		int n = ms.cutTree(id);
		if (n>0) {
			return Result.toClient("0", "删除成功");
		}else {
			return Result.toClient("1", "删除失败");
		}
	}

}
